package com.atguigu.juc.t6;

import java.util.concurrent.locks.ReentrantReadWriteLock;

public class T2 {
    public static void main(String[] args) {
        ReentrantReadWriteLock readWriteLock = new ReentrantReadWriteLock();
        ReentrantReadWriteLock.ReadLock readLock = readWriteLock.readLock();
        ReentrantReadWriteLock.WriteLock writeLock = readWriteLock.writeLock();

//        writeLock.lock();
//        System.out.println("获得写的锁，开始写");
//        readLock.lock();
//        System.out.println("获得读的操作，开始读");
//        writeLock.unlock();
//        System.out.println("释放写的锁");
//        readLock.unlock();
//        System.out.println("释放读的锁");
        //正常运行

        readLock.lock();
        System.out.println("获得读的操作，开始读");
        writeLock.lock();                   //阻塞，因为这个锁不支持升级
        System.out.println("获得写的锁，开始写");
        writeLock.unlock();
        System.out.println("释放写的锁");
        readLock.unlock();
        System.out.println("释放读的锁");
    }
}
